package com.yanghongyue.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yanghongyue.reggie.bean.Dish;
import com.yanghongyue.reggie.bean.Setmeal;
import com.yanghongyue.reggie.bean.SetmealDish;
import com.yanghongyue.reggie.bean.dto.SetmealDto;
import com.yanghongyue.reggie.common.CustException;
import com.yanghongyue.reggie.mapper.SetmealMapper;
import com.yanghongyue.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @author HongyueYang
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishServiceImpl setmealDishService;

    //保存套餐
    @Override
    @Transactional
    public void saveSetmealWithDish(SetmealDto dto) {
        //保存数据到套餐表
        this.save(dto);
        //获取传递过来的菜品集合（用关系表的实体类接收）
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            //设置套餐id
            setmealDish.setSetmealId(dto.getId());
        }
        //批量保存套餐和菜品的关联关系
        setmealDishService.saveBatch(setmealDishes);

    }

    //根据id获取套餐
    @Override
    public SetmealDto getSetmealWithDish(Long id) {
        //获取套餐信息
        Setmeal setmeal = this.getById(id);
        //创建dto对象
        SetmealDto setmealDto = new SetmealDto();
        //复制属性
        BeanUtils.copyProperties(setmeal, setmealDto);
        //根据套餐id查询套餐菜品关系表
        QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id", id);
        List<SetmealDish> setmealDishes = setmealDishService.list(wrapper);
        //给dto对象设置菜品集合
        setmealDto.setSetmealDishes(setmealDishes);
        return setmealDto;
    }

    //修改套餐
    @Override
    @Transactional
    public void updateSetmealWithDish(SetmealDto dto) {
        //修改套餐信息
        this.updateById(dto);
        //根据id删除菜品信息
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("setmeal_id", dto.getId());
        setmealDishService.remove(wrapper);
        //获取dto对象中的菜品集合
        List<SetmealDish> dishes = dto.getSetmealDishes();
        //遍历集合
        for (SetmealDish dish : dishes) {
            //给菜品设置所属套餐id
            dish.setSetmealId(dto.getId());
        }
        //批量保存菜品
        setmealDishService.saveBatch(dishes);
    }

    @Override
    @Transactional
    public void deleteSetmealWithDish(Long[] ids) throws CustException {
        //根据传来的id 查询是否有正在售卖的套餐
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        wrapper.in("id", ids);

        //查询是否有正在售卖的套餐
        int count = this.count(wrapper);
        if (count > 0) {
            System.out.println("符合条件的套餐数量为：" + count);
            throw new CustException("套餐正在售卖中，无法删除");
        }
        //删除套餐
        this.removeByIds(Arrays.asList(ids));
        //根据套餐id删除关系表中的对应关系
        QueryWrapper<SetmealDish> dishQueryWrapper = new QueryWrapper<>();
        dishQueryWrapper.in("setmeal_id", ids);
        //批量删除
        setmealDishService.remove(dishQueryWrapper);
    }


}
